from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.

class Patient(models.Model):
    id = models.BigAutoField(primary_key=True)
    full_name = models.CharField(max_length=100)  # 姓名
    patient_number = models.CharField(max_length=50, unique=True)  # 患者编号，确保唯一

    age = models.IntegerField(null=True, blank=True)  # 年龄
    weight = models.FloatField(null=True, blank=True)  # 体重
    height = models.FloatField(null=True, blank=True)  # 身高，使用 FloatField 存储浮动数值，允许为空
    temperature = models.FloatField(null=True, blank=True)  # 温度，使用 FloatField 存储浮动数值，允许为空
    tdd = models.FloatField(null=True, blank=True)  # 总日剂量(Total Daily Dose)
    basal_insulin = models.FloatField(null=True, blank=True)  # 基础胰岛素剂量


    def __str__(self):
        return self.full_name

    class Meta:
        db_table = 'patient_info'  # 自定义表名


class PatientBolus(models.Model):
    patient_number = models.CharField(max_length=50)  # 患者编号
    time = models.DateTimeField()  # 时间字段
    bolus = models.FloatField()  # bolus 值

    class Meta:
        db_table = 'patient_bolus'  # 所有患者的 bolus 数据都存在这个表里

    def __str__(self):
        return f"Bolus record for {self.patient_number} at {self.time}"


class PatientMeal(models.Model):
    patient_number = models.CharField(max_length=50)  # 患者编号
    time = models.DateTimeField()  # 用餐时间
    meal_type = models.CharField(max_length=20)  # 餐食类型（早餐、午餐、晚餐、加餐等）
    cho = models.FloatField()  # 碳水化合物含量（克）
    pre_meal_bolus = models.FloatField()  # 餐前胰岛素剂量（单位）
    notes = models.TextField(null=True, blank=True)  # 备注信息

    class Meta:
        db_table = 'patient_meal'  # 所有患者的餐食记录都存在这个表里

    def __str__(self):
        return f"Meal record for {self.patient_number} at {self.time}"


class User(AbstractUser):
    role = models.CharField(max_length=20, default='user')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username
